layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;

	var table_filter = 'tuisong_list',
		where_json = {
			_o: 'order_id',
			_b: 'desc'
		},
		form_filter = 'tuisong_search_form';

	// 渲染表格
	layer.load();
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: tuisong_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'tuisong_id',
			_b: 'desc'
		},
		defaultToolbar: ['filter'],
		totalRow: true,
		cols: [tuisong_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 15,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#tuisongToolbar',
		initSort: {
			field: 'order_id',
			type: 'desc'
		},
		done: function() {
			layer.closeAll();

		}
	});

	var shenqingfapiao = function(order_no, kplx, x1, x2) {
		send_info(send_e_invoice_uri, {
			order_no: order_no,
			kplx: kplx,
			fpqqlsh: ''
		}, function(data) {
			if (x1 == x2 - 1) {
				layer.closeAll();
				layer.confirm(data.data, {
					icon: 1
				}, function(i) {
					table.reload(table_filter);
				});
			}
		})
	}
	var shenqingshudian = function(order_no, kplx, x1, x2) {
		send_info(send_sd_invoice_uri, {
			order_no: order_no
		}, function(data) {
			if (x1 == x2 - 1) {
				layer.closeAll();
				layer.confirm(data.data, {
					icon: 1
				}, function(i) {
					table.reload(table_filter);
				});
			}
		})
	}
	var sdhongchong = function(order_no, kplx, x1, x2) {
		send_info(send_sd_hongchong_uri, {
			order_no: order_no
		}, function(data) {
			if (x1 == x2 - 1) {
				layer.closeAll();
				layer.confirm(data.data, {
					icon: 1
				}, function(i) {
					table.reload(table_filter);
				});
			}
		})
	}
	// 导入查询
	const upload_daoru_search = upload.render({
		elem: '.daoru_search',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			send_info(excel_uri + '?file=' + res.data.save_path, {}, function(data) {
				table.reload(table_filter, {
					where: {
						out_order_no_arr: data.data,
						excel: 'true'
					},
					page: {
						curr: 1
					}
				});
				layer.closeAll();
			})

		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	// 导入修改抬头
	const upload_daoru_edit = upload.render({
		elem: '.daoru_edit',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			send_info(excel_title_uri + '?file=' + res.data.save_path, {}, function(data) {
				table.reload(table_filter, {
					where: {
						out_order_no_arr: data.data,
						excel: 'true'
					},
					page: {
						curr: 1
					}
				});
				layer.closeAll();
			})

		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var checkStatus = table.checkStatus(table_filter);
		if (checkStatus.data.length == 0) {
			layer.msg('请选择要操作的信息', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		switch (obj.event) {
			case 'shenhesd':
				layer.confirm("确认审核通过数电发票申请吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						send_info(one_apply_info_url, {
							order_no: e.order_no,
							apply_state: 3,
							nopass_info: ''
						}, function(data) {
							setTimeout(function() {
								shenqingshudian(e.order_no, 0, i,
									checkStatus.data
									.length);
							}, i * 1000);

						})
					})
					return false;

				})
				break;
			case 'shenhe':
				layer.confirm("确认审核通过发票申请吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						send_info(one_apply_info_url, {
							order_no: e.order_no,
							apply_state: 3,
							nopass_info: ''
						}, function(data) {
							setTimeout(function() {
								shenqingfapiao(e.order_no, 0, i,
									checkStatus.data
									.length);
							}, i * 1000);
							//	layer.closeAll();
							//	layer.confirm("审核通过", {
							//		icon: 1
							//	}, function(i) {
							//		table.reload(table_filter);
							//	});

						})
					})
					return false;

				})
				break;
			case 'bohui':
				layer.prompt({
					title: '请输入驳回原因'
				}, function(value, index, elem) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						send_info(one_apply_info_url, {
							order_no: e.order_no,
							apply_state: 4,
							nopass_info: value
						}, function(data) {
							layer.closeAll();
							layer.confirm("驳回成功", {
								icon: 1
							}, function(i) {
								table.reload(table_filter);
							});

						})
					})
				});
				break;
			case 'shenqing':
				layer.confirm("确认申请发票吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						setTimeout(function() {
							shenqingfapiao(e.order_no, 0, i, checkStatus.data
								.length);
						}, i * 1000);
					})
				})
				break;
			case 'shenqingsd':
				layer.confirm("确认申请数电发票吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						setTimeout(function() {
							shenqingshudian(e.order_no);
						}, i * 1000);
					})
				})
				break;
			case 'hongchong':
				layer.confirm("确认申请发票红冲吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						setTimeout(function() {
							shenqingfapiao(e.order_no, 1, i, checkStatus.data
								.length);
						}, i * 1000);
					})
				})
				break;
			case 'hongchongsd':
				layer.confirm("确认申请数电发票红冲吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						setTimeout(function() {
							sdhongchong(e.order_no, 1, i, checkStatus.data
								.length);
						}, i * 1000);
					})
				})
				break;
			case 'baocun':
				layer.confirm("保存到发票到本地吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						send_info(save_location_url, {
							order_no: e.order_no
						}, function(data) {
							layer.closeAll();
							layer.confirm(data.data, {
								icon: 1
							}, function(i) {
								table.reload(table_filter);
							});

						})
					})
				})
				break;
			case 'tuisong':
				layer.confirm("确定推送发票到军网吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					layer.load();
					checkStatus.data.forEach(function(e, i) {
						send_info(push_jun_invoice_url, {
							order_no: e.out_order_no
						}, function(data) {
							layer.closeAll();
							layer.confirm('推送结果:' + data.data, {
								icon: 1
							}, function(i) {
								table.reload(table_filter);
							});

						})
					})
				})
				break;
			default:
				break;
		}
		return false;
	})
	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		switch (id) {
			case 'site_id':
				select_search('site_id', value);
				break;
			case 'order_type':
				select_search('order_type', value);
				break;
			case 'jun_order_state':
				select_search('jun_order_state', value);
				break;
			case 'apply_state':
				select_search('apply_state', value);
				break;
			case 'invoice_state':
				select_search('invoice_state', value);
				break;
			case 'returncode':
				select_search('returncode', value);
				break;
			case 'gys_sh_state':
				select_search('gys_sh_state', value);
				break;
			case 'is_change':
				select_search('is_change', value);
				break;
			case 'api_type':
				select_search('api_type', value);
				break;
			default:
				break;
		}
	});
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search(name, value);
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		if (obj.tr.length > 1) {
			obj.tr = $(obj.tr[0])
		}
		var tr = obj.tr;
		var order_id = data.order_id;
		var order_no = data.order_no;
		var out_order_no = data.out_order_no;
		switch (e) {
			case 'views':
				layer.open({
					type: 2,
					anim: -1,
					title: '订单详情',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '95%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: content_url + '?order_no=' + order_no
				})
				break;
			case 'huoqusd':
				send_info(send_sd_huoqu_uri, {
					order_no: order_no
				}, function(data) {
					layer.closeAll();
					layer.confirm('获取数电：'+data.data, {
						icon: 1
					}, function(i) {
						table.reload(table_filter);
					});
				})
				break;
			case 'shenhesd':
				layer.confirm("确认审核通过数电发票申请吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(one_apply_info_url, {
						order_no: order_no,
						apply_state: 3,
						nopass_info: ''
					}, function(data) {
						shenqingshudian(order_no)
						layer.closeAll();
						layer.confirm(data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});

					})
				})
				break;
			case 'shenhe':
				layer.confirm("确认审核通过发票申请吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(one_apply_info_url, {
						order_no: order_no,
						apply_state: 3,
						nopass_info: ''
					}, function(data) {
						send_info(send_e_invoice_uri, {
							order_no: order_no,
							kplx: 0,
							fpqqlsh: ''
						}, function(data) {

							layer.closeAll();
							layer.confirm(data.data, {
								icon: 1
							}, function(i) {
								table.reload(table_filter);
							});
						})
						//	layer.closeAll();
						//	layer.confirm("审核通过", {
						//		icon: 1
						//	}, function(i) {
						//		table.reload(table_filter);
						//	});

					})
				})
				break;
			case 'bohui':
				layer.prompt({
					title: '请输入驳回原因'
				}, function(value, index, elem) {
					send_info(one_apply_info_url, {
						order_no: order_no,
						apply_state: 4,
						nopass_info: value
					}, function(data) {
						layer.closeAll();
						layer.confirm("驳回成功", {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});

					})
				});
				break;
			case 'shenqing':
				layer.confirm("确认申请发票吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(send_e_invoice_uri, {
						order_no: order_no,
						kplx: 0,
						fpqqlsh: ''
					}, function(data) {

						layer.closeAll();
						layer.confirm(data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});
					})
				})
				break;
			case 'shenqingsd':
				layer.confirm("确认申请数电发票吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(send_sd_invoice_uri, {
						order_no: order_no,
						kplx: 0,
						fpqqlsh: ''
					}, function(data) {

						layer.closeAll();
						layer.confirm(data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});
					})
				})
				break;
			case 'hongchong':
				layer.confirm("确认申请发票红冲吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(send_e_invoice_uri, {
						order_no: order_no,
						kplx: 1,
						fpqqlsh: ''
					}, function(data) {
						layer.closeAll();
						layer.confirm(data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});
					})
				})
				break;
			case 'hongchongsd':
				layer.confirm("确认申请数电发票红冲吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(send_sd_hongchong_uri, {
						order_no: order_no,
						kplx: 1,
						fpqqlsh: ''
					}, function(data) {
						layer.closeAll();
						layer.confirm(data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});
					})
				})
				break;
			case 'baocun':
				layer.confirm("保存到发票到本地吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(save_location_url, {
						order_no: order_no
					}, function(data) {
						layer.closeAll();
						layer.confirm(data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});

					})
				})
				break;
			case 'tuisong':
				layer.confirm("确定推送发票到军网吗?", {
					icon: 3,
					title: '温馨提示'
				}, function(index) {
					send_info(push_jun_invoice_url, {
						order_no: out_order_no
					}, function(data) {
						layer.closeAll();
						layer.confirm('推送结果:' + data.data, {
							icon: 1
						}, function(i) {
							table.reload(table_filter);
						});

					})
				})
				break;
			case 'viewMore':
				if (tr.last().find('i').hasClass('layui-icon-rate-solid')) {
					tr.last().find('i.layui-icon-rate-solid').removeClass('layui-icon-rate-solid')
						.addClass('layui-icon-rate');
					tr.after(
						'<tr data-index="-1"><td colspan="16" class="layui-bg-green" style="padding:1px 1px 1px 5px;"><table id="tuisong_list_son_old' +
						order_id + '" lay-filter="tuisong_list_son_old' + order_id +
						'"></table></td></tr><tr data-index="-2"><td colspan="16" class="layui-bg-blue" style="padding:1px 1px 1px 5px;"><table id="tuisong_list_son' +
						order_id + '" lay-filter="tuisong_list_son' + order_id +
						'"></table></td></tr>');
					table.render({
						elem: '#tuisong_list_son_old' + order_id,
						id: 'tuisong_list_son_old' + order_id,
						height: 'auto',
						width: 'auto',
						cols: [tuisong_goods_cols_invoice_old],
						url: cart_detail_url,
						mothod: 'post',
						height: 'auto',
						where: {
							order_no: order_no,
							old: 'yes'
						}
					});
					table.render({
						elem: '#tuisong_list_son' + order_id,
						id: 'tuisong_list_son' + order_id,
						height: 'auto',
						width: 'auto',
						cols: [tuisong_goods_cols_invoice],
						url: cart_detail_url,
						mothod: 'post',
						height: 'auto',
						where: {
							order_no: order_no
						}
					});
					table.on('edit(tuisong_list_son' + order_id + ')', function(obj) {
						var field = obj.field,
							value = obj.value,
							cart_id = obj.data.cart_id;
						if (field == 'invoice_model') {
							if (value == '') {
								value = ' '
							}
						}
						let post_data = {
							field: field,
							value: value,
							cart_id: cart_id
						};
						send_info(save_cart_info_url, post_data, function(data) {
							layer.msg('保存成功', {
								icon: 1
							});
						});
					});
				} else {
					tr.next().remove();
					tr.next().remove();
					tr.find('.view-more i.layui-icon-rate').removeClass('layui-icon-rate').addClass(
						'layui-icon-rate-solid')
				}
				break;
			case 'viewFp':
				window.open(data.s_invoice_proofs);
				break;
			default:
				break;
		}
	});

	//监听编辑
	var editInvoiceInfo = function(field, value, order_no) {
		let post_json = {
			field: field,
			value: value,
			order_no: order_no
		}
		send_info(save_invoice_field_url, post_json, function(data) {
			if (data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			order_no = obj.data.order_no;
		editInvoiceInfo(field, value, order_no);
	});

	form.on('checkbox(LAY_TABLE_TOOL_COLS)', function(data) {
		console.log(data.elem); //得到checkbox原始DOM对象
		console.log(data.elem.checked); //开关是否开启，true或者false
		console.log(data.value); //开关value值，也可以通过data.elem.value得到
		console.log(data.othis); //得到美化后的DOM对象
	})

	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		where_json[name] = value;

	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
	$('#tuisong_search_btn').on('click', function() {
		layer.load();
		where_json['excel'] = 'false'
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	var select_search = function(field, value) {
		var where = {};
		where[field] = value;
		where_json[field] = value;
	};
	// 入库时间2
	var ruku_time_start_2 = laydate.render({
		elem: 'input[name="ruku_time_start_2"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				ruku_time_end_2.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('ruku_time_start_2', value);
		}
	});
	var ruku_time_end_2 = laydate.render({
		elem: 'input[name="ruku_time_end_2"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				ruku_time_start_2.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('ruku_time_end_2', value);
		}
	});
	// 供应商发票申请时间
	var apply_time_start_2 = laydate.render({
		elem: 'input[name="apply_time_start_2"]',
		max: 0,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_time_end_2.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('apply_time_start_2', value);
		}
	});
	var apply_time_end_2 = laydate.render({
		elem: 'input[name="apply_time_end_2"]',
		max: 0,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_time_start_2.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('apply_time_end_2', value);
		}
	});
	// 供发货时间
	var gys_fh_time_start_2 = laydate.render({
		elem: 'input[name="gys_fh_time_start_2"]',
		max: 0,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_fh_time_end_2.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_fh_time_start_2', value);
		}
	});
	var gys_fh_time_end_2 = laydate.render({
		elem: 'input[name="gys_fh_time_end_2"]',
		max: 0,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_fh_time_start_2.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_fh_time_end_2', value);
		}
	});
	// 供收货时间
	var gys_sh_time_start_2 = laydate.render({
		elem: 'input[name="gys_sh_time_start_2"]',
		max: 0,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_sh_time_end_2.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_sh_time_start_2', value);
		}
	});
	var gys_sh_time_end_2 = laydate.render({
		elem: 'input[name="gys_sh_time_end_2"]',
		max: 0,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_sh_time_end_2.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_sh_time_end_2', value);
		}
	});
	// 用户发票时间
	var invoice_time_start_2 = laydate.render({
		elem: 'input[name="invoice_time_start_2"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				invoice_time_end_2.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('invoice_time_start_2', value);
		}
	});
	var invoice_time_end_2 = laydate.render({
		elem: 'input[name="invoice_time_end_2"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				invoice_time_start_2.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('invoice_time_end_2', value);
		}
	});
	// 供应商发票审核时间
	var apply_shehe_time_start_2 = laydate.render({
		elem: 'input[name="apply_shehe_time_start_2"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_shehe_time_end_2.config.min = {
					year: date.year,
					month: date.month - 1,

					date: date.date
				};
			}
			select_search('apply_shehe_time_start_2', value);
		}
	});
	var apply_shehe_time_end_2 = laydate.render({
		elem: 'input[name="apply_shehe_time_end_2"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_shehe_time_start_2.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('apply_shehe_time_end_2', value);
		}
	});
	// 下单时间
	var create_time_start = laydate.render({
		elem: 'input[name="order_form_create_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_start', value);
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="order_form_create_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_end', value);
		}
	});
});